const path = require('path')
const { merge } = require('webpack-merge')
const commonConfig = require('./webpack.common')
const TerserJSPlugin = require('terser-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')

module.exports = merge(commonConfig, {
  entry: './src/main.js',
  output: {
    path: path.join(__dirname, 'dist'),
    filename: '[hash:8].bundle.js',
    chunkFilename: '[hash:8].chunk.js'
  },
  mode: 'production',
  devtool: 'none',
  optimization: {
    minimizer: [
      new TerserJSPlugin({ // 允许你通过提供一个或多个定制过的 TerserPlugin 实例，覆盖默认压缩工具
        cache: true,
        parallel: true
      }),
      new OptimizeCSSAssetsPlugin({})
    ],
    noEmitOnErrors: true // 在编译出错时，使用 optimization.noEmitOnErrors 来跳过生成阶段(emitting phase)。这可以确保没有生成出错误资源。而 stats 中所有 assets 中的 emitted 标记都是 false。
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: '[name].css',
      chunkFilename: '[id].css'
    })
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'less-loader'
        ]
      }
    ]
  }
})
